#include "../mqserver/mq_consumer.hpp"
#include <gtest/gtest.h>
ns_kkymq::ConsumerManager::ptr cmp;

class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cmp = std::make_shared<ns_kkymq::ConsumerManager>();
        cmp->initQueueConsumer("queue1");
    }
    virtual void TearDown() override
    {
        // emp->clear();
    }
};

void ConsumerCallback(const std::string &tag, const ns_kkymq::BasicProperties *bp, const std::string &body)
{
    std::cout << tag << "消费了消息:" << body << std::endl;
}

TEST(consumer_test, insert_test)
{
    cmp->create("comsumer1", "queue1", false, ConsumerCallback);
    cmp->create("comsumer2", "queue1", false, ConsumerCallback);
    cmp->create("comsumer3", "queue1", false, ConsumerCallback);

    ASSERT_EQ(cmp->exists("comsumer1", "queue1"), true);
    ASSERT_EQ(cmp->exists("comsumer2", "queue1"), true);
    ASSERT_EQ(cmp->exists("comsumer3", "queue1"), true);
}

TEST(consumer_test, remove_test)
{
    cmp->remove("comsumer1", "queue1");

    ASSERT_EQ(cmp->exists("comsumer1", "queue1"), false);
    ASSERT_EQ(cmp->exists("comsumer2", "queue1"), true);
    ASSERT_EQ(cmp->exists("comsumer3", "queue1"), true);
}

TEST(consumer_test, choose_test)
{
    ns_kkymq::Consumer::ptr cp1 = cmp->choose("queue1");
    ASSERT_NE(cp1.get(), nullptr);
    ASSERT_EQ(cp1->_tag, "comsumer2");

    ns_kkymq::Consumer::ptr cp2 = cmp->choose("queue1");
    ASSERT_NE(cp2.get(), nullptr);
    ASSERT_EQ(cp2->_tag, "comsumer3");

    ns_kkymq::Consumer::ptr cp3 = cmp->choose("queue1");
    ASSERT_NE(cp3.get(), nullptr);
    ASSERT_EQ(cp3->_tag, "comsumer2");
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    RUN_ALL_TESTS();
    return 0;
}
